//
//  SYS_SelectGoodsCell.m
//  System_App
//
//  Created by 吴健 on 2017/12/5.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_SelectGoodsCell.h"

@interface SYS_SelectGoodsCell ()
{
    IBOutlet UIImageView *imv_head;
    IBOutlet UILabel     *lb_title;
    IBOutlet UILabel     *lb_currentPrice;
    IBOutlet UILabel     *lb_originalPrice;
    IBOutlet UILabel     *lb_standard;
    IBOutlet UILabel     *lb_deliveryWay;
    IBOutlet UIButton    *btn_small;
    IBOutlet UIButton    *btn_big;
    IBOutlet NSLayoutConstraint *lc_titleHeight;
    IBOutlet NSLayoutConstraint *lc_priceWidth;
    IBOutlet NSLayoutConstraint *lc_originalPriceWidth;
    __weak IBOutlet UILabel *num;
}
@end


@implementation SYS_SelectGoodsCell

+ (SYS_SelectGoodsCell *)getSelectGoodsCell{
    SYS_SelectGoodsCell *cell = nil;
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"SYS_SelectGoodsCell" owner:self options:nil];
    for (NSObject *obj in objects) {
        if ([obj isKindOfClass:[SYS_SelectGoodsCell class]]) {
            cell = (SYS_SelectGoodsCell *)obj;
            break;
        }
    }
    return cell;
}

// iOS 11的 margins
- (UIEdgeInsets)layoutMargins {
    [super layoutMargins];
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
    btn_big.tag = btn_small.tag = _indexPath.section;
}

- (void)setModel:(OrderItems *)model{
    _model = model;
    [imv_head sd_setImageWithURL:[NSURL URLWithString:_model.logo] placeholderImage:WJIMAGE(@"good_icon")];
    lb_title.text = _model.itemName;
    
    //现价（目前后台只存price）
    lb_currentPrice.text = FORMAT(@"￥%.2f", _model.price.floatValue);
    lc_priceWidth.constant = [DSUtility getStringWidth:lb_currentPrice.text front:DSFONT_DETAIL];
    //原价
    lb_originalPrice.text = FORMAT(@"￥%.2f", _model.goodsSpecValuesPrice.doubleValue);
    lb_originalPrice.attributedText = [[NSAttributedString alloc] initWithString:lb_originalPrice.text attributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle), NSBaselineOffsetAttributeName: @(0),NSStrikethroughColorAttributeName: WJCOLOR_GRAY, NSFontAttributeName: DSFONT_DETAIL}];
    lc_originalPriceWidth.constant = [DSUtility getStringWidth:lb_originalPrice.text front:DSFONT_DETAIL];
    //型号  尺寸
    lb_standard.text = _model.specValue;
    num.text=[NSString stringWithFormat:@"x%@",_model.count];
    
    //    [btn_small addTarget:self action:@selector(doSelectGoodsClicked:) forControlEvents:UIControlEventTouchUpInside];
    //    [btn_big addTarget:self action:@selector(doSelectGoodsClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    btn_small.selected=YES;
    lc_titleHeight.constant = [DSUtility heightOfString:lb_title.text font:DSFONT width:SCREENWIDTH-60 - 90 - lc_priceWidth.constant maxHeight:50];
    
    lb_deliveryWay.text = FORMAT(@"配送方式：%@", _model.name);
    [self updateConstraintsIfNeeded];
}

//- (void)setDeliveryWayArray:(NSArray *)deliveryWayArray{
//    _deliveryWayArray = deliveryWayArray;
//
//    for (int i = 0; i < _deliveryWayArray.count; i++) {
//        DeliveryWayList *listModel = _deliveryWayArray[i];
//        if ([listModel.deliveryWayId isEqualToString:_model.deliveryWayId]) {
//            lb_deliveryWay.text = FORMAT(@"配送方式：%@", listModel.name);
//        }
//    }
//}

#pragma mark -- touch and event
- (void)doSelectGoodsClicked:(UIButton *)sender{
    NSLog(@"++++++%ld",sender.tag);
    sender.selected = !sender.selected;
    btn_small.selected = sender.selected;
    if ([_delegate respondsToSelector:@selector(choseTerm:)]) {
        [_delegate choseTerm:sender];
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
